/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.sys.entity;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.entity.BaseEntity;
import com.jeesite.common.entity.DataEntity;
import com.jeesite.common.entity.Extend;
import com.jeesite.common.entity.TreeEntity;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.mybatis.annotation.Column;
import com.jeesite.common.mybatis.annotation.JoinTable;
import com.jeesite.common.mybatis.annotation.JoinTable.Type;
import com.jeesite.common.mybatis.annotation.Table;
import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.base.entity.guarantor.Guarantor;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * 公司管理Entity
 *
 * @author ThinkGem
 * @version 2017-03-23
 */
@Table(name = "${_prefix}sys_company", alias = "a", columns = {
        @Column(includeEntity = BaseEntity.class),
        @Column(includeEntity = DataEntity.class),
        @Column(includeEntity = TreeEntity.class),
        @Column(name = "company_code", attrName = "companyCode", label = "公司编码",queryType = QueryType.LIKE, isPK = true),
        @Column(name = "view_code", attrName = "viewCode", label = "公司代码",queryType = QueryType.LIKE),
        @Column(name = "company_name", attrName = "companyName", label = "公司名称", queryType = QueryType.LIKE, isTreeName = true),
        @Column(name = "full_name", attrName = "fullName", label = "公司全称", queryType = QueryType.LIKE),
        @Column(name = "area_code", attrName = "area.areaCode", label = "区域编码"),
        @Column(name = "company_address", attrName = "companyAddress", label = "详细地址"),
        @Column(name = "default_guarantor", attrName = "defaultGuarantor.guarantorId", label = "默认担保人"),
        @Column(name = "tax_number", attrName = "taxNumber", label = "税号"),
        @Column(name = "contact_person", attrName = "contactPerson", label = "联系人"),
        @Column(name = "email", attrName = "email", label = "邮箱"),
        @Column(name = "phone", attrName = "phone", label = "电话"),
        @Column(name = "fax", attrName = "fax", label = "传真"),
        @Column(name = "contact_person_addr", attrName = "contactPersonAddr", label = "联系人地址"),
        @Column(includeEntity = Extend.class, attrName = "extend"),
}, joinTable = {
        @JoinTable(type = Type.LEFT_JOIN, entity = Area.class, alias = "b",
                on = "b.area_code = a.area_code",
                columns = {
                        @Column(name = "area_code", label = "区域代码", isPK = true),
                        @Column(name = "area_name", label = "区域名称", isQuery = false),
                        @Column(name = "area_type", label = "区域类型"),
                        @Column(name = "tree_names", label = "区域全称"),
                }),
        @JoinTable(type = JoinTable.Type.LEFT_JOIN, entity = Guarantor.class, attrName = "defaultGuarantor", alias = "g",
                on = "g.guarantor_id = a.default_guarantor", columns = {
                @Column(name = "guarantor_id", label = "担保人编码", isPK = true),
                @Column(name = "guarantor_name", label = "担保人名称", isQuery = false),
                @Column(name = "address", label = "地址", isQuery = false),
                @Column(name = "phone", label = "电话", isQuery = false),
                @Column(name = "id_number", label = "身份证号码", isQuery = false),
        }),
}, extWhereKeys = "dsf", orderBy = "a.tree_sorts, a.company_code"
)
public class Company extends TreeEntity<Company> {

    private static final long serialVersionUID = 1L;
    private String companyCode;        // 公司编码
    private String viewCode;        // 公司代码
    private String companyName;        // 公司名称
    private String fullName;        // 公司全称
    private Area area;                // 区域编码
    private Extend extend;            // 扩展字段
    private String companyAddress;
    private Guarantor defaultGuarantor;
    private String taxNumber;
    private String contactPerson;
    private String email;
    private String phone;
    private String fax;
    private String contactPersonAddr;


    private List<CompanyOffice> companyOfficeList = ListUtils.newArrayList();    // 公司所包含的部门信息

    public Company() {
        this(null);
    }

    public Company(String id) {
        super(id);
    }

    @Override
    public Company getParent() {
        return parent;
    }

    @Override
    public void setParent(Company parent) {
        this.parent = parent;
    }

    public String getCompanyCode() {
        return companyCode;
    }

    public void setCompanyCode(String companyCode) {
        this.companyCode = companyCode;
    }

    @NotBlank(message = "公司编号不能为空")
    @Pattern(regexp = "[a-zA-Z0-9_]{0,30}", message = "代码长度不能大于 30 个字符，并且只能包含字母、数字、下划线")
    public String getViewCode() {
        return viewCode;
    }

    public void setViewCode(String viewCode) {
        this.viewCode = viewCode;
    }

    @NotBlank(message = "公司名称不能为空")
    @Length(min = 0, max = 200, message = "公司名称长度不能超过 200 个字符")
    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

//    @NotBlank(message = "公司全称不能为空")
    @Length(min = 0, max = 200, message = "公司全称长度不能超过 200 个字符")
    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Area getArea() {
        return area;
    }

    public void setArea(Area area) {
        this.area = area;
    }

    public Extend getExtend() {
        return extend;
    }

    public void setExtend(Extend extend) {
        this.extend = extend;
    }

    public List<CompanyOffice> getCompanyOfficeList() {
        return companyOfficeList;
    }

    public void setCompanyOfficeListJson(String jsonString) {
        List<String> list = JsonMapper.fromJson(jsonString, List.class);
        if (list != null) {
            for (String val : list) {
                if (StringUtils.isNotBlank(val)) {
                    CompanyOffice e = new CompanyOffice();
                    e.setCompanyCode(this.companyCode);
                    e.setOfficeCode(val);
                    e.setIsNewRecord(true);
                    this.companyOfficeList.add(e);
                }
            }
        }
    }

    public String getCompanyAddress() {
        return companyAddress;
    }

    public void setCompanyAddress(String companyAddress) {
        this.companyAddress = companyAddress;
    }

    public String getTaxNumber() {
        return taxNumber;
    }

    public void setTaxNumber(String taxNumber) {
        this.taxNumber = taxNumber;
    }

    public String getContactPerson() {
        return contactPerson;
    }

    public void setContactPerson(String contactPerson) {
        this.contactPerson = contactPerson;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getContactPersonAddr() {
        return contactPersonAddr;
    }

    public void setContactPersonAddr(String contactPersonAddr) {
        this.contactPersonAddr = contactPersonAddr;
    }

    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    public Guarantor getDefaultGuarantor() {
        return defaultGuarantor;
    }

    public void setDefaultGuarantor(Guarantor defaultGuarantor) {
        this.defaultGuarantor = defaultGuarantor;
    }

    @Override
    public String toString() {
        return "Company{" +
                "companyCode='" + companyCode + '\'' +
                ", viewCode='" + viewCode + '\'' +
                ", companyName='" + companyName + '\'' +
                ", fullName='" + fullName + '\'' +
                ", area=" + area +
                ", extend=" + extend +
                ", companyAddress='" + companyAddress + '\'' +
                ", defaultGuarantor=" + defaultGuarantor +
                ", taxNumber='" + taxNumber + '\'' +
                ", contactPerson='" + contactPerson + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", fax='" + fax + '\'' +
                ", contactPersonAddr='" + contactPersonAddr + '\'' +
                ", companyOfficeList=" + companyOfficeList +
                '}';
    }
}